package com.quick.calcu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quick.calcu.entity.AnswerRecord;
import com.quick.calcu.entity.AnswerStats;
import com.quick.calcu.entity.ClassRecord;
import com.quick.calcu.entity.Student;
import com.quick.calcu.mapper.AnswerRecordMapper;
import com.quick.calcu.service.AnswerRecordService;
import com.quick.calcu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class AnswerRecordServiceImpl extends ServiceImpl<AnswerRecordMapper, AnswerRecord> implements AnswerRecordService {

    @Autowired
    private StudentService studentService;

    @Override
    public List<AnswerRecord> getRecordsByStudentId(Long studentId) {
        LambdaQueryWrapper<AnswerRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AnswerRecord::getStudentId, studentId);
        return list(wrapper);
    }

    @Override
    public AnswerStats getStudentStats(Long studentId) {
        List<AnswerRecord> records = getRecordsByStudentId(studentId);

        int totalQuestions = 0;
        int correctQuestions = 0;

        for (AnswerRecord record : records) {
            totalQuestions += record.getCount();
            correctQuestions += record.getCountCorrect();
        }

        double accuracyRate = totalQuestions > 0 ?
                (double) correctQuestions / totalQuestions : 0.0;

        AnswerStats stats = new AnswerStats();
        stats.setTotalQuestions(totalQuestions);
        stats.setCorrectQuestions(correctQuestions);
        stats.setAccuracyRate(accuracyRate);

        return stats;
    }

    @Override
    public List<AnswerRecord> getRecordByStudentId(long studentId) {
        LambdaQueryWrapper<AnswerRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AnswerRecord::getStudentId, studentId);
        return list(wrapper);
    }

    //根据学生id和日期查询答题记录
    @Override
    public List<AnswerRecord> getRecordByStudentIdAndTime(long studentId, LocalDate date) {
        LambdaQueryWrapper<AnswerRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AnswerRecord::getStudentId, studentId);
        wrapper.eq(AnswerRecord::getSubmitTime, date);
        List<AnswerRecord> records = list(wrapper);
        if(records.isEmpty()){
            return null;
        }else{
            for(AnswerRecord record:records){
                record.setStudent(studentService.getById(record.getStudentId()));
            }
        }
        return records;
    }



    @Override
    public ClassRecord getRecordByClassId(long classId, LocalDate time) {
        System.out.println("classId:"+classId+" time:"+time);
        ClassRecord classRecord = new ClassRecord();
        List<Student> students = studentService.getStudentsByClassId(classId);
        int totalCount=0;
        int correctCount=0;
        //根据学生id查询答题记录
        List<List<AnswerRecord>> totalRecords = new java.util.ArrayList<>();
        for (Student student : students) {
            List<AnswerRecord> records = getRecordByStudentIdAndTime(student.getStudentId(),time);
            if(records!=null) {
                totalRecords.add( records);
                for (AnswerRecord record : records) {
                    if (record != null) {
                        totalCount += record.getCount();
                        correctCount += record.getCountCorrect();
                    }
                }
            }
        }
        classRecord.setRecords(totalRecords);
        classRecord.setTotalCount(totalCount);
        classRecord.setCorrectCount(correctCount);
        if(totalCount==0){
            classRecord.setAverageRate(0);
        }else{
            classRecord.setAverageRate((float) correctCount /totalCount);
        }
        return classRecord;
    }
}